package com.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

import com.model.*;
import com.opensymphony.xwork2.ActionContext;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

public class Register {


	private String firstname;
	private String lastname;
	private String homeaddress;
	private String city;
	private String state;
	private String pincode;
	private String countryname;
	private String phoneno;
	private String email;
	private String ebayuserid;
	private String password;
	private String secretques;
	private String answer;
	private Date dob;
	private Map session;
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getHomeaddress() {
		return homeaddress;
	}
	public void setHomeaddress(String homeaddress) {
		this.homeaddress = homeaddress;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getPincode() {
		return pincode;
	}
	public void setPincode(String pincode) {
		this.pincode = pincode;
	}
	
	public String getPhoneno() {
		return phoneno;
	}
	public void setPhoneno(String phoneno) {
		this.phoneno = phoneno;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEbayuserid() {
		return ebayuserid;
	}
	public void setEbayuserid(String ebayuserid) {
		this.ebayuserid = ebayuserid;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getCountryname() {
		return countryname;
	}
	public void setCountryname(String countryname) {
		this.countryname = countryname;
	}
	public String getSecretques() {
		return secretques;
	}
	public void setSecretques(String secretques) {
		this.secretques = secretques;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public Date getDob() {
		return dob;
	}
	public void setDob(Date dob) {
		this.dob = dob;
	}
	
	 public String execute()throws Exception{
			HttpServletRequest request = ServletActionContext.getRequest();
			String remoteAddr = request.getRemoteAddr();
	        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
	        reCaptcha.setPrivateKey("6LcJIs8SAAAAADUp1meZJnbEdoYpLzB0BQrv9Ex9");

	        String challenge = request.getParameter("recaptcha_challenge_field");
	        String uresponse = request.getParameter("recaptcha_response_field");
	        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
	     
	        if (!reCaptchaResponse.isValid()) {
	        	return "error";
	        }
		 
		 session=ActionContext.getContext().getSession();
		 session.put("email", email);
		 
		 
		 Registermodel rm = new  Registermodel();
		 rm.setFirstname(firstname);
		 rm.setLastname(lastname);
		 rm.setHomeaddress(homeaddress);
		 rm.setCity(city);
		 rm.setState(state);
		 rm.setPincode(pincode);
		 rm.setCountryname(countryname);
		 rm.setPhoneno(phoneno);
		 System.out.println("Email id is "+email);
		 rm.setEmail(email);
		 rm.setEbayuserid(ebayuserid);
		 rm.setPassword(password);
		 rm.setSecretques(secretques);
		 rm.setAnswer(answer);
		 rm.setDob(dob);
		 rm.setActive(0);
	
		 int r=rm.insert();
		 
		  Emailer em=new Emailer();
		  String body=null, from =null;
		  from="kadamdeepali17@gmail.com";
		  em.setFrom(from);
		  em.setTo(email);
		  em.setSubject("Welcome to Ebay ");
		  em.setPassword("8880806481");
		  
		  
		  body="Enjoy Shopping wth eBay!!!!\n Now buy YOUR choice items\n To activate Ur account click following link \n"+"<a href=\"http://localhost:8080/Ebay/link.jsp?email="+ email +"\"> Activate UR eBay Account </a>";
		  
		  System.out.println("result:    "+body);
		  
		  em.setBody(body);
		  em.sendEmail();
		 
		 return "SUCCESS";
	 
	 }
	
}
